package cn.zhihong.config;

import cn.zhihong.common.JacksonObjectMapper;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * @ClassName WebMvcConfiguration
 * @Author 庄志宏
 * @Date 2022/4/20 12:48
 */
@Configuration
@MapperScan("cn.zhihong.mapper")
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {
    /**
     * @author 庄志宏
     * @date: 2022/4/21 19:39
     * 加载静态资源
     */
        protected void addResourceHandlers(ResourceHandlerRegistry registry) {
            log.info("开始静态资源映射====");
            registry.addResourceHandler("/**")
                    .addResourceLocations("classpath:/")
                    .addResourceLocations("classpath:/public/")
                    .addResourceLocations("classpath:/static/")
                    .addResourceLocations("classpath:/resources/")
                    .addResourceLocations("classpath:/META-INF/resources/");
            registry.addResourceHandler("/webjars/**")
                    .addResourceLocations("classpath:/META-INF/resources/webjars/");
        }
        /**
         * @author 庄志宏
         * @date: 2022/4/21 20:06
         * 扩展MVC框架的消息转换器
         */
        @Override
        protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
            //创建消息转换器对象
            MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
            //设置对象转换器（即对象映射器），底层使用Jackson将java对象转为json对象
            jackson2HttpMessageConverter.setObjectMapper(new JacksonObjectMapper());
            //将上面的消息转换器对象追加到MVC框架的转换器集合中，才会生效
            converters.add(0,jackson2HttpMessageConverter);
        }
}
